package com.purang.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.purang.web.converter.DtoConverter;
import com.purang.web.entity.vo.ResponseVo;
import com.purang.web.entity.vo.UserVo;
import com.purang.web.service.TestService;
import com.purang.web.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试案例
 *
 * @author Zheming Liu
 * @version 1.0-RELEASE
 * @since 1.0-RELEASE
 */
@Slf4j
@RestController
public class TestController {

    private static final DtoConverter CONVERTER = DtoConverter.INSTANCE;

    @Autowired
    private TestService testService;

    @GetMapping("/test")
    public ResponseVo test() {
        Map<String, String> params = new HashMap<>();
        params.put("username", "root");
        params.put("password", "root");
        params.put("gender", "女");
        UserVo userVo = new UserVo("root", "root", "女");
        JSONObject getResult = HttpClientUtils
                .builder()
                .get(params, true)
                .uri("/getTest")
                .build()
                .executeHttp();
        JSONObject xFormResult = HttpClientUtils
                .builder()
                .xForm(params)
                .uri("/xFormTest")
                .build()
                .executeHttp();
        JSONObject bodyResult = HttpClientUtils
                .builder()
                .body(JSON.toJSONString(userVo))
                .uri("/bodyTest")
                .build()
                .executeHttp();
        JSONObject multipartFormResult = HttpClientUtils
                .builder()
                .multipartForm(params)
                .uri("/multipartFormTest")
                .build()
                .executeHttp();
        return ResponseVo.ok();
    }

    @GetMapping("/getTest")
    public ResponseVo getTest(@Validated UserVo user) {
        log.info("The request param user-{}", user);
        testService.insertUser(CONVERTER.convert(user));
        return ResponseVo.ok();
    }

    @PostMapping(value = "/xFormTest", consumes = "application/x-www-form-urlencoded;charset=utf-8", produces = "application/json;charset=utf-8")
    public ResponseVo xFormTest(@Validated UserVo user) {
        log.info("The request param user-{}", user);
        testService.insertUser(CONVERTER.convert(user));
        return ResponseVo.ok();
    }

    @PostMapping(value = "/bodyTest", consumes = "application/json;charset=utf-8", produces = "application/json;charset=utf-8")
    public ResponseVo bodyTest(@RequestBody @Validated UserVo user) {
        log.info("The request param user-{}", user);
        testService.insertUser(CONVERTER.convert(user));
        return ResponseVo.ok();
    }

    @PostMapping(value = "/multipartFormTest", consumes = "multipart/form-data;charset=utf-8", produces = "application/json;charset=utf-8")
    public ResponseVo multipartFormTest(@Validated UserVo user) {
        log.info("The request param user-{}", user);
        testService.insertUser(CONVERTER.convert(user));
        return ResponseVo.ok();
    }
}
